﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Tanis.Settings;

namespace Tanis.Controls
{
	public partial class FavoritesControl : UserControl
	{
		public FavoritesControl()
		{
			InitializeComponent();

            _favouriteRoutes.ItemsSource = FavouriteRouteList;
		}

        private ObservableCollection <RouteQuery> _favouriteRouteList;

        public string TrashImagePath 
        { 
            get 
            {
                if (ThemeHelper.ThemeDependantColor == Colors.White)
                {
                    return "/Tanis;component/Images/appbar.delete.rest.black.png";
                }
                else
                {
                    return "/Tanis;component/Images/appbar.delete.rest.png";
                }
            } 
        }

        /// <summary>
        /// User favourite routes
        /// </summary>
        public ObservableCollection<RouteQuery> FavouriteRouteList
        {
            get
            {
                if (_favouriteRouteList == null)
                {
                    _favouriteRouteList = SettingsControl.GetApplicationSetting("FavouriteRoutes") as ObservableCollection<RouteQuery>;
                }

                if (_favouriteRouteList == null)
                {
                    _favouriteRouteList = new ObservableCollection<RouteQuery>();
                }

                return _favouriteRouteList;
            }
        }

        /// <summary>
        /// Adds a new favourite route and saves in phone memory
        /// </summary>
        /// <param name="query"></param>
        public void AddFavouriteRoute(RouteQuery query)
        {
            _favouriteRouteList.Add(query);
            MainPage.MainPageInstance.RouteFinder.FavouriteSearchPerformed = true;
            SettingsControl.SetApplicationSetting("FavouriteRoutes", _favouriteRouteList);

            if (MainPage.MainPageInstance != null)
            {
                // navigate to favourites after adding new route //
                MainPage.MainPageInstance.UpdatePanelVisibility();
                MainPage.MainPageInstance._pivot.SelectedItem = MainPage.MainPageInstance._favouriteRouteCtrl;
            }
        }

        /// <summary>
        /// Event when user taps on favourite route from the list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Tap(object sender, GestureEventArgs e)
        {
            // find out which route has been selected //
            string buttonName = ((TextBlock) ((Button) sender).Content).Text;
            RouteQuery route = FavouriteRouteList.FirstOrDefault(x => x.Name == buttonName);

            MainPage.MainPageInstance.RouteFinder.FavouriteSearchPerformed = true;

            App.ViewModel.OnSearchPerfoming();
            MainPage.MainPageInstance.RouteFinder.SetRouteQuery(route);
            MainPage.MainPageInstance.RouteFinder.PerformSearch();
            App.ViewModel.OnSearchPerfomed();
        }

        /// <summary>
        /// Deletes a favourite route
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Tap_1(object sender, GestureEventArgs e)
        {
            RouteQuery selectedFavoriteRoute = _favouriteRouteList.FirstOrDefault(x => x.Name == ((Button)sender).Tag.ToString());

            MessageBoxResult confirmation = MessageBox.Show(
                String.Format(Localization.LocalizedStringsRes.FavouriteRouteDeleteConfirmationMessage, selectedFavoriteRoute.Name),
                Localization.LocalizedStringsRes.WarningTitle,
                MessageBoxButton.OKCancel);            

            if (confirmation == MessageBoxResult.OK)
            {
                _favouriteRouteList.Remove(selectedFavoriteRoute);
                MainPage.MainPageInstance.UpdatePanelVisibility();
            }
        }

        public bool ContainsRoute(string routeName)
        {
            RouteQuery route = _favouriteRouteList.FirstOrDefault(x => x.Name == routeName);
            if (route != null)
            {
                return true;
            }

            return false;
        }
    }
}